package thread;

/**
 * @User: vitobo
 * @Date: 2024-07-25
 * @Description: 线程中断: 自定义标记
 */
public class ThreadDemo8 {
    private static boolean flag = true;
    public static void main(String[] args) throws InterruptedException {
        Thread t = new Thread(()->{
           while (flag){
               System.out.println("hello thread");
               try {
                   Thread.sleep(1000);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
           }
        });

        t.start();

        Thread.sleep(3000);
        // 在主线程里 可以随时通过flag变量的取值, 来操作t线程是否结束
        flag = false;
    }
}
